$(function () {
    // 配置路径
    var pageUrl = config.urlAnaConfig.testPath;
    var PathLoginUrl = config.urlAnaConfig.PathLogin;
    // 获取Cookie
    function getCookie(name){
        var strcookie = document.cookie;//获取cookie字符串
        var arrcookie = strcookie.split("; ");//分割
    //       console.log(arrcookie);
        //遍历匹配
        for ( var i = 0; i < arrcookie.length; i++) {
            var arr = arrcookie[i].split("=");
                if (arr[0] ==name){
                    return arr[1];
                }
        }
        return "";
    }
    var name = "tokenval";
    // tiken值
    var cookie_value = getCookie(name);
    console.log(cookie_value);
    var name1 = "userName";
    // 用户名值
    var cookie_value1 = getCookie(name1);
    $(".userName").html(cookie_value1);
    $(".nav-usename").html(cookie_value1);
    /*选货*/


/*    // hover效果
    $(".main1-con ul li").click(function(){
        $(".TK").show();
        $(".closeBtn").show();
        $(".TK").find(".show-tit").html($(this).find("p").html());
    });
    $(".close").click(function () {
        $(this).parents(".TK").hide();
        $(this).parent(".TK").find(".show-tit").html()=='';
    });*/


    // 下拉数据
     $.ajax({
        url: pageUrl+'/api/selectproduct/getplatforms',
        type: 'POST',
        dataType: 'json',
        headers: {
            Authorization: 'Bearer '+cookie_value
        },
        success: function (data) {
            var msg = '';
            if (data) {
                if (data.code === 200) {
    //                  店铺名称
                    var details1 = data.data.channels;
                    var details1F = $(".details1").next('ul');
                    var mydetails1 = details1;
                    /*for in 循环*/
                     for(var i in mydetails1){
                         var text="<li>" + mydetails1[i] + " </li>";
                         details1F.append(text);
                     };
                     $('.det_det').on('click','li',function(){
                        $(".details2 option:gt(0)").remove();
                        $(".details3 option:gt(0)").remove();
                        $(".details2").attr("disabled",false);
                        $(".details3").attr("disabled",false);
                        var details1D= $(this).html();
                        console.log(details1D);
                        $.ajax({
                            url: pageUrl+'/api/selectproduct/getstorebyplatform',
                            type: 'POST',
                            dataType: 'json',
                            data:{
                                platform:details1D
                            },
                            headers: {
                                Authorization: 'Bearer '+cookie_value
                            },
                            success: function (data) {
                                var msg = '';
                                if (data) {
                                    if (data.code === 200) {
                        //                  店铺名称
                                        var details2 = data.data.stores;
                                        console.log(details2);
                                        var details2F = $(".details2").next('ul');
                                        var mydetails2 = details2;
                                        /*for in 循环*/
                                         for(var i in mydetails2){
                                             var text="<li>" + mydetails2[i] + " </li>";
                                             details2F.append(text);
                                         };

                                    }
                                }
                            },
                            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

                        });
                        $.ajax({
                            url: pageUrl+'/api/selectproduct/getdivision',
                            type: 'POST',
                            dataType: 'json',
                            data:{
                                platform:details1D
                            },
                            headers: {
                                Authorization: 'Bearer '+cookie_value
                            },
                            success: function (data) {
                                var msg = '';
                                if (data) {
                                    if (data.code === 200) {
                        //                  店铺名称
                                        var details3 = data.data.divisions;
                                        console.log(details3);
                                        var details3F = $(".details3").next('ul');
                                        var mydetails3 = details3;
                                        /*for in 循环*/
                                         for(var i in mydetails3){
                                             var text="<li>" + mydetails3[i] + " </li>";
                                             details3F.append(text);
                                         };

                                    }
                                }
                            },
                            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

                        });
                    });

                }
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });

     $.ajax({
        url: pageUrl+'/api/common/getaccesstoken',
        type: 'POST',
        dataType: 'json',
        success: function (data) {
            var msg = '';
            if (data) {
                var accessToken = data.data.accessToken.value;
                console.log(accessToken+111);
                var embedUrl = "https://app.powerbi.cn/reportEmbed?reportId=c7a4219b-ba99-4838-83cd-a6fdacc233f0&groupId=13e42e94-3404-46d8-b605-228c35984df3";
                xuanhuo(accessToken,embedUrl);
                var embedUrl1 = "https://app.powerbi.cn/reportEmbed?reportId=f40dae56-14ba-42d2-a8fc-e2439b75bbb3&groupId=13e42e94-3404-46d8-b605-228c35984df3";
                tu1(accessToken,embedUrl1);
            }
        },
        error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

    });
     // 图
     function xuanhuo(accessToken,embedUrl) {
         // Read embed application token from Model
        // var accessToken = "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IkZDQmFtTFdha2tLWVhTMFFEdDJYcVFha2JPRSIsImtpZCI6IkZDQmFtTFdha2tLWVhTMFFEdDJYcVFha2JPRSJ9.eyJhdWQiOiJodHRwczovL2FuYWx5c2lzLmNoaW5hY2xvdWRhcGkuY24vcG93ZXJiaS9hcGkiLCJpc3MiOiJodHRwczovL3N0cy5jaGluYWNsb3VkYXBpLmNuLzlhZWJiZmVkLTc1MmMtNDVhYy1iZDhiLTg2ODczODZiYjg0ZS8iLCJpYXQiOjE1NTIwMzQxMzIsIm5iZiI6MTU1MjAzNDEzMiwiZXhwIjoxNTUyMDM4MDMyLCJhY2N0IjowLCJhY3IiOiIxIiw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W5hLmNuIiwidXBuIjoiemlrYWlAc2FwenoucGFydG5lci5vbm1zY2hpbmEuY24iLCJ1dGkiOiJBX3lEZktFblFreUEwVGhEaUpjRUFBIiwidmVyIjoiMS4wIiwid2lkcyI6WyI2MmU5MDM5NC02OWY1LTQyMzctOTE5MC0wMTIxNzcxNDVlMTAiXX0.SiWEqHYzOtWUc0aS4I1nBlKn5AQayDQfB5BWE664PM8tGGQrI8DCDQg4APCXn0skl-Ap7tTDq97yOxRfMw6j6DCEjgRAD80cp-XEJcHnnYHsGyyU01Wac0ZtKEZiaW0uUDAy5vBUnQF1HZ6hDd0Ur50oxeDR1oomSt2Asiy5Rq4OfcPOmnroERc2rFra-L2vQ9DLzmHetVxqswIbNp0HoGr3-gfNMeNaO0ps9kxBt-Zg8tpxNOEMHm5h-75oZnEPhEbSgxja2vrefNlFDytFzRu0rnQSi0iKYGfkq9sJ0pRNk36_wk881kOI4gRWUEtOo3z292QEZ871Qg6qJklMNw";
        // Read embed URL from Model
         accessToken = accessToken;

        embedUrl = embedUrl;
        console.log(embedUrl);
        // Read report Id from Model
        var embedReportId = "c7a4219b-ba99-4838-83cd-a6fdacc233f0";
        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;
        // Embed configuration used to describe the what and how to embed.
        // This object is used when calling powerbi.embed.
        // This also includes settings and options such as filters.
        // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
        var config = {
            type: 'report',
            // tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId,
            viewMode: models.ViewMode.View,
            // filters: filter,
            permissions: models.Permissions.All ,
            settings: {
                filterPaneEnabled: false,
                navContentPaneEnabled: false
            }
        };
        // Get a reference to the embedded report HTML element
        var tu11 = $('.tu11')[0];
        if ("" != "") {
            $("#RLS").prop('checked', true);
            $("#RLSdiv").show();
        }
        else
        {
            $("#RLS").prop('checked', false);
            $("#RLSdiv").hide();
        }
        if ("False" == "True") {
            $("#noRLSdiv").hide();
            $("#RLS").removeAttr("disabled");
            $("#RLS").change(function () {
                if ($(this).is(":checked")) {
                    $("#RLSdiv").show(300);
                } else {
                    $("#RLSdiv").hide(200);
                }
            });
        }
        else
        {
            $("#noRLSdiv").show();
        }
        var report = powerbi.embed(tu11, config);
     };
    // 图1
    function tu1(accessToken,embedUrl1) {
        accessToken = accessToken;
        embedUrl = embedUrl1;
        console.log(embedUrl);
        // Read report Id from Model
        var embedReportId = "f40dae56-14ba-42d2-a8fc-e2439b75bbb3";
        // Get models. models contains enums that can be used.
        var models = window['powerbi-client'].models;
        // Embed configuration used to describe the what and how to embed.
        // This object is used when calling powerbi.embed.
        // This also includes settings and options such as filters.
        // You can find more information at https://github.com/Microsoft/PowerBI-JavaScript/wiki/Embed-Configuration-Details.
        var config = {
            type: 'report',
            // tokenType: models.TokenType.Embed,
            accessToken: accessToken,
            embedUrl: embedUrl,
            id: embedReportId,
            viewMode: models.ViewMode.View,
            // filters: filter,
            permissions: models.Permissions.All ,
            settings: {
                filterPaneEnabled: false,
                navContentPaneEnabled: false
            }
        };
        // Get a reference to the embedded report HTML element
        var tu22 = $('.tu22')[0];
        if ("" != "") {
            $("#RLS").prop('checked', true);
            $("#RLSdiv").show();
        }
        else
        {
            $("#RLS").prop('checked', false);
            $("#RLSdiv").hide();
        }
        if ("False" == "True") {
            $("#noRLSdiv").hide();
            $("#RLS").removeAttr("disabled");
            $("#RLS").change(function () {
                if ($(this).is(":checked")) {
                    $("#RLSdiv").show(300);
                } else {
                    $("#RLSdiv").hide(200);
                }
            });
        }
        else
        {
            $("#noRLSdiv").show();
        }
        var report = powerbi.embed(tu22, config);
    };
     // 确定
     var pingjieIndex1 = 0;
     $(".application").on("click",function () {
         var platform = $(".details1").val();
         var store = $(".details2").val();
         var division = $(".details3").val();
         var data_date = $(".details4").val();
         var data_date1 = $(".details5").val();
        if (platform == 0 || store==0 || division==0 || data_date==0 || data_date1==0){
            alert("请选择完整选项");
            return false;
        };
         // 拼接图形盒子1
        pingjieIndex1++;
        var tu1Id = "reportContainer";
        tu1Id = tu1Id + pingjieIndex1;
        console.log(tu1Id);
        var arrtx1 = [];
        arrtx1.push("<div class='tu11 no-border'" + "id=" +tu1Id +">");
        arrtx1.push("</div>");
        str0 = arrtx1.join('');
        $(".tu1").append(str0);
        // 删除其他元素
        $('.tu1'+' #'+tu1Id).siblings().remove();
         var embedUrl = "https://app.powerbi.cn/reportEmbed?reportId=c7a4219b-ba99-4838-83cd-a6fdacc233f0&groupId=13e42e94-3404-46d8-b605-228c35984df3";
         embedUrl = embedUrl+"&filter=data_date/data_date ge "+ data_date +" and data_date/data_date le "+ data_date1+" and platform/platform eq "+"'"+platform+"'"+ " and store/store eq "+"'"+store+"'"+" and division/division eq "+"'"+division+"'";
         $.ajax({
            url: pageUrl+'/api/common/getaccesstoken',
            type: 'POST',
            dataType: 'json',
            success: function (data) {
                var msg = '';
                if (data) {
                    var accessToken = data.data.accessToken.value;
                    console.log(accessToken);
                    xuanhuo(accessToken,embedUrl);
                }
            },
            error:  function (XMLHttpRequest, textStatus, errorThrown) {
                    // msg
                    var errorTips = XMLHttpRequest.responseJSON.msg;
                    if (errorTips=='Division by zero'){
                        $(".layer1 .content").html("数据错误");
                        $(".layer1").show();
                        console.log(errorTips);
                    } else if(errorTips=='Token has expired'){
                        $(".layer1 .content").html("令牌过期");
                        $(".layer1").show();
                        window.location.href="login.html";
                    }


                }

        });
     });

     // 日历插件
    //  $('#datetimepicker').datetimepicker('setStartDate', null);
    //  $('#datetimepicker').datetimepicker('setEndDate', '2020-01-01');
    $('.form_date').datetimepicker({
        language:  'zh-CN',
        weekStart: 0,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight:true,
        startView: 2,
        minView: 2,
        forceParse: 0,
    
        forceParse:true,
      
        /*      endDate:newDay*/
    });

    $(".form_date").change(function(){
        var sTimeData = $(this).val();
        $('.form_date1').datetimepicker('setStartDate', sTimeData);
    });
    $('.form_date1').datetimepicker({
        language:  'zh-CN',
        weekStart: 0,
        todayBtn:  1,
        autoclose: 1,
        todayHighlight:true,
        startView: 2,
        minView: 2,
        forceParse: 0,
    
        forceParse:true,
      
        /*      endDate:newDay*/
    });
});
